﻿using KELT.KELTObjects.ViewportObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace KELT.Animation.Animation2D
{


    public class PulseAnimation : IAnimation
    {
        private DoubleAnimationUsingKeyFrames opacityAni;

        private int option = 5;

        public PulseAnimation()
        {

            //칼라애니메이션 설정
            opacityAni = new DoubleAnimationUsingKeyFrames();

        }
        public override void SetAnimation(Storyboard myStoryboard, ViewportObj obj)
        {

            double animationTime = endTime - startTime;
            opacityAni.Duration = TimeSpan.FromSeconds(animationTime);
            opacityAni.BeginTime = TimeSpan.FromSeconds(startTime);


            for (int i = 1; i < option; i++)
            {
                double time = (animationTime / option) * i;
                opacityAni.KeyFrames.Add(
                    new LinearDoubleKeyFrame(
                        0.0, // Target value (KeyValue)
                        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(time))) // KeyTime
                    );

                opacityAni.KeyFrames.Add(
                new LinearDoubleKeyFrame(
                    1.0, // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(time + 0.1))) // KeyTime
                );
            }




            Storyboard.SetTargetName(opacityAni, AnimationInfo.ANIMATION_ALPHA + obj.getIndexNumber());
            Storyboard.SetTargetProperty(opacityAni, new PropertyPath(SolidColorBrush.OpacityProperty));
            myStoryboard.Children.Add(opacityAni);


        }

        public override void changeAnimation(Storyboard myStoryboard)
        {

            myStoryboard.Children.Remove(opacityAni);

            opacityAni.Duration = TimeSpan.FromSeconds(endTime - startTime);
            opacityAni.BeginTime = TimeSpan.FromSeconds(startTime);


            double animationTime = endTime - startTime;

            opacityAni.KeyFrames.Clear();
            for (int i = 0; i < option; i++)
            {
                double time = (animationTime / option) * i;
                opacityAni.KeyFrames.Add(
                    new LinearDoubleKeyFrame(
                        1.0, // Target value (KeyValue)
                        KeyTime.FromTimeSpan(TimeSpan.FromSeconds(time))) // KeyTime
                    );

                opacityAni.KeyFrames.Add(
                new LinearDoubleKeyFrame(
                    0.0, // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(time + 0.1))) // KeyTime
                );
            }
            myStoryboard.Children.Add(opacityAni);
        }


        public override void deleteAnimation(Storyboard myStoryboard)
        {
            myStoryboard.Children.Remove(opacityAni);
        }
        
        public void setOption(int count)
        {
            option = count;
        }


    }

}
